﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;

public partial class Operator_Payments : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();


    protected decimal getPayedMainAmount(int contractId)
    {
        var payments = from payment in db.ContractPayments where payment.ConractId == contractId select payment;
        decimal payedAmount = 0;
        foreach (ContractPayment contractPayment in payments)
        {
            payedAmount += contractPayment.MainAmount;
        }
        return payedAmount;
    }

    protected decimal getPayedPercentageAmount(int contractId)
    {
        var payments = from payment in db.ContractPayments where payment.ConractId == contractId select payment;
        decimal payedAmount = 0;
        foreach (ContractPayment contractPayment in payments)
        {
            payedAmount += contractPayment.PercentageAmount;
        }
        return payedAmount;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridViewContracts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            LinkButton link = (LinkButton)e.Row.FindControl("PayLink");
            link.CommandArgument = e.Row.Cells[0].Text;
            int contractId = Convert.ToInt32(e.Row.Cells[0].Text);
            e.Row.Cells[6].Text = getPayedMainAmount(contractId).ToString();
            e.Row.Cells[7].Text = getPayedPercentageAmount(contractId).ToString();
        }
    }

    protected void PayClicked(object sender, EventArgs e)
    {
        LinkButton link = (LinkButton)sender;
        Session.Add("contractId", link.CommandArgument);
        Response.Redirect("~/Operator/PayContract.aspx");
    }

    protected void FindContractButton_Click(object sender, EventArgs e)
    {
        try
        {
            Convert.ToInt32(ContractNumber.Text);
            SqlDataSourceContracts.SelectParameters["ContractId"].DefaultValue = ContractNumber.Text;
            GridViewContracts.DataBind();
            GridViewContracts.Visible = true;
        }
        catch
        {

        }
    }
}